/*
 * @Author: lzx lzx@gonsin.cn
 * @Date: 2023-01-05 09:42:49
 * @LastEditors: lzx lzx@gonsin.cn
 * @LastEditTime: 2023-01-09 16:33:26
 * @FilePath: \gonsin-i18n\i18n.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */


import Vue from 'vue'
import VueI18n, { LocaleMessages } from 'vue-i18n'
import axios from 'axios'
import AesManager from "./utils/AesManager"

Vue.use(VueI18n)

export const config = {
  PROJECT: "paperless",
  IP: "",
  PORT: "",
  KEY: "nusTIdVQFhQs2lrz"
}

const loadedLanguages: string[] = []

// function loadLocaleMessages(): LocaleMessages {

//     // const locales = require.context('./locales', true, /[A-Za-z0-9-_,\s]+\.json$/i)
//     const messages: LocaleMessages = {}
//     // locales.keys().forEach(key => {
//     //   const matched = key.match(/([A-Za-z0-9-_]+)\./i)
//     //   if (matched && matched.length > 1) {
//     //     const locale = matched[1]
//     //     messages[locale] = locales(key)
//     //   }
//     // })
//     return messages
//   }

const i18n = new VueI18n({
  locale: "zh-CN",
  fallbackLocale: "zh-CN",
  messages: {
    "zh-CN": require("./locales/cn.json"),
  },// loadLocaleMessages()
})


// 动态设置服务器中的json资源
function setI18nLanguage(lang: string): string {
  i18n.locale = lang
  // axios.defaults.headers.common['Accept-Language'] = lang
  // document.querySelector('html')?.setAttribute('lang', lang)
  return lang
}


export function loadLanguageAsync(lang: string) {
  axios.post(`http://${config.IP}:${config.PORT}/api/i18n/${config.PROJECT}/${lang}`).then(response => {
    const json = AesManager.decrypt(config.KEY, response.data);
    const result = JSON.parse(json);
    loadedLanguages.push(lang)
    i18n.setLocaleMessage(lang, result.data)
    setI18nLanguage(lang)
  })
  setI18nLanguage(lang)
}


export const T = {
  setConfigProject(name:string){
    config.PROJECT = name;
  },
  load(lang: string) {
    loadLanguageAsync(lang)
  },
  t(text: string) {
    let arr = i18n.messages
    if (typeof (arr[i18n.locale][text]) == 'undefined') {
      //上传
      arr[i18n.locale][text] = text;
      const json = AesManager.encrypt(config.KEY, text);
      axios.post(`http://${config.IP}:${config.PORT}/api/i18n/${config.PROJECT}/${i18n.locale}?keyword=${json}`).then(response => {

      })
      return text
    } else {
      return arr[i18n.locale][text]
    }
  },
  text(text: string, common: string) {
    let arr = i18n.messages
    if (typeof (arr[i18n.locale][text]) == 'undefined') {
      //上传
      arr[i18n.locale][text] = text;
      const json = AesManager.encrypt(config.KEY, text);
      if (common) {
        axios.post(`http://${config.IP}:${config.PORT}/api/i18n/${config.PROJECT}/${i18n.locale}?keyword=${json}&common=${common}`).then(response => {

        })
      } else {
        axios.post(`http://${config.IP}:${config.PORT}/api/i18n/${config.PROJECT}/${i18n.locale}?keyword=${json}`).then(response => {

        })
      }
      return text
    } else {
      return arr[i18n.locale][text]
    }
  }
}

export default i18n